﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using DBAccessLibrary;
using DBAccessLibrary.Model;
using SQLQuery.Common;

namespace SQLQuery.UI
{
    /// <summary>
    /// AddDescriptionWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddDescriptionWindow
    {
        readonly TableEntity _table;
        public AddDescriptionWindow(TableEntity table)
        {
            _table = table;
            InitializeComponent();
            InitControl(table);
            XmlHelper.Instance.SetTheme(this);
        }

        private void InitControl(TableEntity table)
        {
            this.grid.Children.Clear();
            int count = table.FileNodes.Count();
            //2列
            this.grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Auto)});
            this.grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Auto) });

            //增加 表名称 在第一行
            DockPanel dp = GetItemElement(
                   table.TransName,
                   table.TransChName,
                   0,
                   0);
            this.grid.Children.Add(dp);
            //N行
            for (int i = 0; i < count / 2; i++)
            {
                this.grid.RowDefinitions.Add(new RowDefinition { Height =  new GridLength(35) });
            }
            FieldEntity[] files = table.FileNodes.ToArray();
            for (int i = 0; i < count; i++)
            {
                FieldEntity field = files[i];
                if (field != null)
                    dp = GetItemElement(
                        field.TransName,
                        field.TransChName,
                        (i /2) + 1 , 
                        i % 2);
                this.grid.Children.Add(dp);
            }
        }

        private DockPanel GetItemElement(string fieldName, string chName,int rowindex,int colunindex)
        {
            DockPanel panel = new DockPanel();
            Label label = new Label { Content = fieldName, MinWidth = 100, MaxWidth = 100, Margin = new Thickness(5, 5, 2, 5) };
            TextBox tb = new TextBox { Text = chName,Margin= new Thickness(0,5,5,5)};
            panel.Children.Add(label);
            panel.Children.Add(tb);
            panel.SetValue(Grid.RowProperty, rowindex);
            panel.SetValue(Grid.ColumnProperty, colunindex);
            return panel;
        }

        private void BtnSubmitClick(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (DockPanel dp in this.grid.Children)
                {
                    string fieldname = ((Label) dp.Children[0]).Content.ToString();
                    string chfieldname = ((TextBox) dp.Children[1]).Text;
                    if (chfieldname.Trim() == string.Empty)
                        continue;
                    if (fieldname == _table.TransName)
                    {
                        _table.TransChName = chfieldname;
                    }
                    else
                    {
                        FieldEntity field = _table.FileNodes.FirstOrDefault(o => o.TransName == fieldname) as FieldEntity;
                        //如果 描述 相等 则不做更新
                        //if (field.TransChName != chfieldname)
                        //{
                        if (field != null)
                        {
                            DBOperatorFactory.GetOperator.AddDescription(_table.TransName,
                                                                           fieldname, _table.TransChName, chfieldname,
                                                                           field.TypeName, field.Length,field.IsPrimaryKey);
                            field.TransChName = chfieldname;
                        }
                        //}
                    }
                }
                MessageBoxHelper.Show("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBoxHelper.Show("保存失败,详细:"+ex.Message);
            }
        }

        private void BtnCanelClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void DxWindowKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                BtnSubmitClick(null, null);
            }
            else if (e.Key == System.Windows.Input.Key.Escape)
            {
                BtnCanelClick(null, null);
            }
        }
    }
}
